<template>
	<view class="login-limit-layout">
		<view class="login-limit-box">
			<view class="login-limit-item">
				<text class="login-limit-item-title">多人同时登录</text>
				<view class="switch-right">
					<uv-radio-group v-model="formParams.radiovalue" labelSize="28rpx" iconSize="20rpx" size="30rpx">
						<uv-radio name="1" label="开启" :customStyle="{marginRight:'40rpx'}"></uv-radio>
						<uv-radio name="0" label="关闭"></uv-radio>
					</uv-radio-group>
				</view>
				<view class="line"></view>
			</view>
			<view class="login-limit-item">
				<text class="login-limit-item-title">允许同时登录人数</text>
				<view class="switch-right">
					<xh-input placeholder="请输入数量" keyboardType="number" v-model="formParams.count" inputTextAlign="center"></xh-input>
					<text>人</text>
				</view>
				<view class="line"></view>
			</view>
		</view>
		<view class="submit-btn" :style="{marginTop:'50rpx'}" @tap="submit">提交</view>
		<xh-alert ref="alertRef" @submit="submitMultiLoginSet"></xh-alert>
	</view>
</template>

<script setup>
	import { ref } from 'vue'
	import { onLoad } from '@dcloudio/uni-app'
	import { useUserStore } from '@/common/stores'
	import { getUserInfo,setMultiLoginStatus } from '@/common/api/api.js'
	
	const userStore = useUserStore()
	const alertRef = ref(null)
	const formParams = ref({
		radiovalue:'0',
		count:''
	})
	//获取用户信息
	const updateUserInfo = async ()=>{
		const res = await getUserInfo()
		userStore.user = {...userStore.user,...res.result}
		formParams.value.radiovalue = userStore.user.manyFlag
		formParams.value.count = userStore.user.maxLogin
	}
	// 提交
	const submit = async ()=>{
		if(uni.$uv.test.empty(formParams.value.count) && formParams.value.radiovalue == '1'){
			uni.$utils.showToast('请输入允许同时登录人数')
			return
		}
		else if(parseInt(formParams.value.count) <= 0 && formParams.value.radiovalue == '1'){
			uni.$utils.showToast('输入的允许同时登录人数必须大于0')
			return
		}
		else{
			if(formParams.value.radiovalue == '0'){
				alertRef.value.open({content:'关闭多人同时登录，需重新登录账号'})
			}
			else{
				submitMultiLoginSet()
			}
		}
	}
	//确认提交
	const submitMultiLoginSet = async ()=>{
		let params = {}
		params.openFlag = formParams.value.radiovalue
		if(formParams.value.radiovalue == '1'){
			params.maxLogin = parseInt(formParams.value.count)
		}
		await setMultiLoginStatus(params)
		updateUserInfo()
		if(formParams.value.radiovalue == '1'){
			uni.$utils.showToast('提交成功')
			uni.navigateBack()
		}
	}
	onLoad(()=> {
		updateUserInfo()
	})
</script>

<style lang="scss" scoped>
	.login-limit-layout {
		.login-limit-box{
			background-color: #ffffff;
			.login-limit-item{
				display: flex;
				align-items: center;
				justify-content: space-between;
				height: 100rpx;
				padding: 0 30rpx;
				position: relative;
				.login-limit-item-title{
					flex-shrink: 0;
				}
				.switch-right{
					display: flex;
					align-items: center;
				}
			}
		}
		
	}
</style>